package com.starchen.optimalselection.product.controller;

import com.starchen.optimalselection.common.utils.Result;
import com.starchen.optimalselection.product.model.CategoryBrandRelation;
import com.starchen.optimalselection.product.model.vo.CategoryBrandRelationVo;
import com.starchen.optimalselection.product.service.CategoryBrandRelationService;
import com.starchen.optimalselection.common.model.Brand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌分类关联
 *
 * @author daxilong
 * @email daxilong@gmail.com
 * @date 2022-08-14 19:57:36
 */

@Api(tags = "分类和品牌关联表 控制器")
@RestController
@RequestMapping("/admin/categorybrandrelation")
public class CategoryBrandRelationController {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @ApiOperation("根据品牌ID获取品牌关联的素有属性分类信息")
    @GetMapping("/list")
    public Result list(Long brandId){
        List<CategoryBrandRelationVo> relationEntityList = categoryBrandRelationService.getRelationByBrand(brandId);

        return Result.success(relationEntityList);
    }

    @ApiOperation("根据品牌ID获取品牌关联的素有属性分类信息")
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Long id){
        CategoryBrandRelation categoryBrandRelation = categoryBrandRelationService.getById(id);

        return Result.success(categoryBrandRelation);
    }

    @ApiOperation("新增品牌和分类关联信息")
    @PostMapping("/create")
    public Result save(@RequestBody CategoryBrandRelation categoryBrandRelation){
        categoryBrandRelationService.save(categoryBrandRelation);
        return Result.success();
    }

    @ApiOperation("删除品牌和分类关联信息")
    @DeleteMapping("/delete")
    public Result delete(Long id){
        categoryBrandRelationService.removeById(id);
        return Result.success();
    }

    @ApiOperation("根据分类ID，获取所有品牌信息")
    @GetMapping("/brands")
    public Result relationBrandsList(Long categoryId){
        List<Brand> brandList = categoryBrandRelationService.getRelationByCategory(categoryId);
        return Result.success(brandList);
    }

}
